package com.itheima.array;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

//System.currentTimeMillis();
public class Demo01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        String choice;
        lo:
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生成绩");
            System.out.println("2 删除学生记录");
            System.out.println("3 修改学生信息");
            System.out.println("4 查看学生信息");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            choice = sc.next();
            switch (choice) {
                case "1":
                    System.out.println("添加学生成绩");
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    System.out.println("修改学生信息");
                    updataStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输入有误,请重新输入:");
                    break;
            }
        }
    }


    //获取所输入学号的学生对象集合中的索引
    public static int getIndex(ArrayList<Student> list, String sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (sid.equals(list.get(i).getSid())) {
                index = i;
                break;
            }
        }
        return index;
    }

    //添加学生成绩
    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号:");
        String sid;
        while (true) {
            sid = sc.next();
            if (getIndex(list, sid) < 0) {
                break;
            }else{
                System.out.println("该学号已被占用,请重新输入:");
            }
        }
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入成绩:");
        double score = sc.nextDouble();
        Student stu = new Student(sid, name, age, score);
        list.add(stu);
        System.out.println("信息录入成功");
    }

    //删除学生记录
    private static void deleteStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无信息,无法进行删除操作");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除的学生学号:");
        String sid = sc.next();
        int index = getIndex(list, sid);
        if (index >= 0) {
            list.remove(index);
            System.out.println("信息删除成功");
            return;
        } else {
            System.out.println("输入错误,删除操作失败");
        }
    }

    //修改学生信息
    private static void updataStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无信息,无法进行修改操作");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学生学号:");
        String sid = sc.next();
        int index = getIndex(list, sid);
        if (index >= 0) {
            System.out.println("请输入新的姓名");
            String newName = sc.next();
            System.out.println("请输入新的年龄");
            int newAge = sc.nextInt();
            System.out.println("请输入新的分数");
            double newScore = sc.nextDouble();
            Student stu = new Student(sid, newName, newAge, newScore);
            list.set(index, stu);
            System.out.println("信息修改成功");
            return;
        } else {
            System.out.println("输入错误,修改操作失败");
        }
    }

    //查看学生信息
    private static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无信息,请添加后再次查询");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t成绩");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getSid() + "\t" + list.get(i).getName() + "\t" + list.get(i).getAge() + "\t\t" + list.get(i).getScore());
        }

    }
}
